package com.taxi.service;

import com.taxi.constant.IdentityConstant;
import com.taxi.constant.OrderConstants;
import com.taxi.dto.OrderDetailInfo;
import com.taxi.dto.OrderInfo;
import com.taxi.remote.ServiceOrderInfoClient;
import com.taxi.remote.ServiceSsePushClient;
import com.taxi.request.OrderRequest;
import com.taxi.request.PushRequest;
import com.taxi.result.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @Author: aniu
 * @Date: 2023/10/11
 * @ClassName: OrderInfoService
 * @Description: Write class functionality and purpose here.
 * @Version: 1.0
 */
@Service
public class ApiDriverOrderInfoService {
    @Autowired
    private ServiceOrderInfoClient orderInfoClient;
    @Autowired
    private ServiceSsePushClient serviceSsePushClient;

    public Result toPickUpPassenger(OrderRequest orderRequest) {
        OrderInfo orderInfo = orderInfoClient.toPickUpPassenger(orderRequest).getData();
        // 通知乘客
        JSONObject passengerContent = new JSONObject();
        passengerContent.put("orderId",orderInfo.getId());
        passengerContent.put("driverId", orderInfo.getDriverId());
        passengerContent.put("orderStatus", orderInfo.getOrderStatus());
        passengerContent.put("currentLongitude",orderInfo.getReceiveOrderCarLongitude());
        passengerContent.put("currentLatitude",orderInfo.getReceiveOrderCarLatitude());

        PushRequest pushRequest = new PushRequest();
        pushRequest.setUserId(orderInfo.getPassengerId());
        pushRequest.setIdentity(IdentityConstant.PASSENGER_IDENTITY);
        pushRequest.setContent(passengerContent.toString());
        serviceSsePushClient.push(pushRequest);
        return Result.ok();
    }


    public Result arrivedDeparture(OrderRequest orderRequest) {
        OrderInfo orderInfo = orderInfoClient.arrivedDeparture(orderRequest).getData();
        // 通知乘客
        JSONObject passengerContent = new JSONObject();
        passengerContent.put("orderId",orderInfo.getId());
        passengerContent.put("driverId", orderInfo.getDriverId());
        passengerContent.put("orderStatus",orderInfo.getOrderStatus());
        passengerContent.put("currentLongitude",orderInfo.getDepLongitude());
        passengerContent.put("currentLatitude",orderInfo.getDepLatitude());

        PushRequest pushRequest = new PushRequest();
        pushRequest.setUserId(orderInfo.getPassengerId());
        pushRequest.setIdentity(IdentityConstant.PASSENGER_IDENTITY);
        pushRequest.setContent(passengerContent.toString());
        serviceSsePushClient.push(pushRequest);
        return Result.ok();
    }

    public Result pickUpPassenger(OrderRequest orderRequest) {
        OrderInfo orderInfo = orderInfoClient.pickUpPassenger(orderRequest).getData();
        // 通知乘客
        JSONObject passengerContent = new JSONObject();
        passengerContent.put("orderId",orderInfo.getId());
        passengerContent.put("driverId", orderInfo.getDriverId());
        passengerContent.put("orderStatus",orderInfo.getOrderStatus());
        passengerContent.put("currentLongitude",orderInfo.getDepLongitude());
        passengerContent.put("currentLatitude",orderInfo.getDepLatitude());

        PushRequest pushRequest = new PushRequest();
        pushRequest.setUserId(orderInfo.getPassengerId());
        pushRequest.setIdentity(IdentityConstant.PASSENGER_IDENTITY);
        pushRequest.setContent(passengerContent.toString());
        serviceSsePushClient.push(pushRequest);
        return Result.ok();
    }

    public Result passengerGetOff(OrderRequest orderRequest) {
        OrderInfo orderInfo = orderInfoClient.passengerGetOff(orderRequest).getData();
        // 通知乘客
        JSONObject passengerContent = new JSONObject();
        passengerContent.put("orderId",orderInfo.getId());
        passengerContent.put("driverId", orderInfo.getDriverId());
        passengerContent.put("orderStatus",orderInfo.getOrderStatus());
        passengerContent.put("currentLongitude",orderInfo.getDestLongitude());
        passengerContent.put("currentLatitude",orderInfo.getDestLatitude());

        PushRequest pushRequest = new PushRequest();
        pushRequest.setUserId(orderInfo.getPassengerId());
        pushRequest.setIdentity(IdentityConstant.PASSENGER_IDENTITY);
        pushRequest.setContent(passengerContent.toString());
        serviceSsePushClient.push(pushRequest);
        return Result.ok();
    }

    public Result cancel(Long orderId) {
        return orderInfoClient.cancel(orderId, IdentityConstant.DRIVER_IDENTITY);
    }

    public Result<OrderInfo> currentOrder(String phone, String driverIdentity) {
        return orderInfoClient.current(phone,driverIdentity);
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    public Result<OrderInfo> detail(Long orderId) {
        return orderInfoClient.detail(orderId);
    }

    public Result<OrderDetailInfo> currentOrderDetail(Long orderId){
        return orderInfoClient.currentOrderDetail(orderId);
    }

    public Result<ArrayList<OrderInfo>> getAllOrders(String phone, String passengerIdentity) {
        return orderInfoClient.getAllOrders(phone,passengerIdentity);
    }
}
